import time
import vec

WIDTH = 250
HEIGHT = 250

BACK_COLOR = (0, 0, 127)
CIRCLE_COLOR = (0, 200, 0)

lastTime = time.time()
deltaTime = 0
spd = 50

way_pt = [vec.Vec2(50, 50), vec.Vec2(50, 200), vec.Vec2(200, 200), vec.Vec2(200, 50)]
next_pt_index = 1
way_dir = way_pt[1] - way_pt[0]
way_dir.normalize()

class MyCircle:
    def __init__(self, pos, r):
        self.pos = pos
        self.r = r

    def Draw(self):
        screen.draw.filled_circle((self.pos.x, self.pos.y), self.r, CIRCLE_COLOR)

c = MyCircle(way_pt[0], 30)

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()

def on_mouse_down(pos):
    print("mouse_down", pos)

def on_mouse_up(pos):
    print("mouse_up", pos)

def move(way_dir, end_pt):
    global deltaTime, spd, c
    next_pt = c.pos + way_dir * deltaTime * spd
    new_dir = end_pt - next_pt
    if new_dir.dot(way_dir) > 0:
        c.pos = next_pt
        return True

    return False

def update():
    global lastTime, deltaTime, next_pt_index, way_dir
    curTime = time.time()
    deltaTime = curTime - lastTime
    lastTime = curTime
    
    if move(way_dir, way_pt[next_pt_index]) == False:
        next_pt_index += 1
        if next_pt_index >= len(way_pt):
            next_pt_index = 0

        if next_pt_index > 0:
            last_pt_index = next_pt_index - 1
        else:
            last_pt_index = len(way_pt) - 1
        way_dir = way_pt[next_pt_index] - way_pt[last_pt_index]
        way_dir.normalize()

def draw():
    screen.fill(BACK_COLOR)
    c.Draw()